<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <link href="./lib/qunit.css" rel="stylesheet" />
</head>
<body>
    <h1 id="qunit-header">QUnit/es5-shim-partial Test</h1>
    <h2 id="qunit-banner"></h2>
    <div id="qunit-testrunner-toolbar"></div>
    <h2 id="qunit-userAgent"></h2>
    <ol id="qunit-tests"></ol>
    <div id="qunit-fixture">test markup</div>
    <script src="./lib/qunit.js"></script>

    <script src="../src/es5plus-shim-partial.js"></script>
    <script type="text/javascript">
        test('Function.prototype.bind', function(){
            var scope = {
                x: 'new scope'
            };
            var orig = function(){
                return this.x;
            };
            var bound = orig.bind(scope);
            equal(bound(), scope.x, '验证this绑定：直接调用');
            equal(bound.apply({x: 'scope of apply'}), scope.x, '验证this绑定：apply');
            equal(bound.call({x: 'scope of call'}), scope.x, '验证this绑定：call');

            orig = function(a, b, c, d){
                return [].slice.call(arguments).join('');
            };
            bound = orig.bind(null, 1, 2);
            equal(bound(3,4), '1234', '验证预设入参');

            equal(bound.length, 2, '验证形参数目');
            deepEqual(/^function[^()]*\((.*?)\)/i.exec(bound.toString())[1].trim().split(','), ['c', ' d'], '验证形参名称一致性（仅polyfill时为真）')

            scope = {
                x: 'x',
                y: 'y'
            };
            orig = function(x, y){
                this.x = x;
                this.y = y;
            };
            bound = orig.bind(scope);
            var ins = new bound(1,2);
            ok(ins.x === 1 && ins.y === 2 && scope.x === 'x' && scope.y === 'y', '构造函数，无预设实参');

            bound = orig.bind(scope, 1);
            ins = new bound(1,2);
            ok(ins.x === 1 && ins.y === 1 && scope.x === 'x' && scope.y === 'y', '构造函数，有预设实参');
        });

        test('String.prototype.trim', function(){
            equal(' content   '.trim(), 'content', '两边均有空格');
            equal(' content'.trim(), 'content', '左边有空格');
            equal('content   '.trim(), 'content', '右边有空格');
        });
        test('String.prototype.trimLeft', function(){
            equal(' content   '.trimLeft(), 'content   ', '两边均有空格');
            equal(' content'.trimLeft(), 'content', '左边有空格');
            equal('content   '.trimLeft(), 'content   ', '右边有空格');
        });
        test('String.prototype.trimRight', function(){
            equal(' content   '.trimRight(), ' content', '两边均有空格');
            equal(' content'.trimRight(), ' content', '左边有空格');
            equal('content   '.trimRight(), 'content', '右边有空格');
        });

        test('Array.isArray', function(){
            ok(Array.isArray([]), '[]');
            ok(!Array.isArray({}), '{}');
            ok(!Array.isArray(1), '1');
            ok(!Array.isArray(null), 'nulll');
            ok(!Array.isArray(false), 'false');
            ok(!Array.isArray('content'), '"content"');
            ok(!Array.isArray(undefined), 'undefined');
            ok(!Array.isArray(new Date()), 'new Date');
            ok(!Array.isArray(/\s/g), '/\\s/g');
            ok(!Array.isArray(document.getElementsByTagName('div')), 'document.getElementsByTagName("div")');
        });
        test('Array.prototype.indexOf', function(){
            var names = ['john', 'marry', 'cat', 'kate', 'cat'];
            equal(names.indexOf('john'), 0, '简单匹配1');
            equal(names.indexOf('cat'), 2, '简单匹配2');
            equal(names.indexOf('john', 0), 0, '设置搜索的起始位置1');
            equal(names.indexOf('john', 1), -1, '设置搜索的起始位置2');
            equal(names.indexOf('kate', -2), 3, '设置搜索的起始位置3');
            equal(names.indexOf('kate', 3), 3, '设置搜索的起始位置4');
            equal(names.indexOf('kate', 5), -1, '设置搜索的起始位置5');
            equal(names.indexOf('cat', 0), 2, '设置搜索的起始位置6');
            equal(names.indexOf('cat', 3), 4, '设置搜索的起始位置7');
            equal(names.indexOf('cat', '3'), 4, '设置搜索的起始位置8');
            equal(names.indexOf('cat', [3]), 4, '设置搜索的起始位置9');
            equal(names.indexOf('john', true), -1, '设置搜索的起始位置10');
            equal(names.indexOf('john', '1s'), 0, '设置搜索的起始位置11');
        });
        test('Array.prototype.lastIndexOf', function(){
            var names = ['john', 'marry', 'cat', 'kate', 'cat'];
            equal(names.lastIndexOf('john'), 0, '简单匹配1');
            equal(names.lastIndexOf('cat'), 4, '简单匹配2');
            equal(names.lastIndexOf('john', 4), 0, '设置搜索的起始位置1');
            equal(names.lastIndexOf('john', 1), 0, '设置搜索的起始位置2');
            equal(names.lastIndexOf('kate', -1), 3, '设置搜索的起始位置3');
            equal(names.lastIndexOf('kate', 3), 3, '设置搜索的起始位置4');
            equal(names.lastIndexOf('kate', 2), -1, '设置搜索的起始位置5');
            equal(names.lastIndexOf('cat', 0), -1, '设置搜索的起始位置6');
            equal(names.lastIndexOf('cat', 3), 2, '设置搜索的起始位置7');
            equal(names.lastIndexOf('cat', -1), 4, '设置搜索的起始位置8');
            equal(names.lastIndexOf('cat', [-1]), 4, '设置搜索的起始位置9');
            equal(names.lastIndexOf('cat', '-1'), 4, '设置搜索的起始位置10');
            equal(names.lastIndexOf('cat', false), -1, '设置搜索的起始位置11');
            equal(names.lastIndexOf('marry', 's'), -1, '设置搜索的起始位置12');
            equal(names.lastIndexOf('john', 's'), 0, '设置搜索的起始位置13');
        });
        test('Array.prototype.forEach', function(){
            var names = [1,2,3];
            var i = 0;
            var ret = names.forEach(function(el, index, array){
                equal(el, ++i, 'el' + i);
                equal(index, i - 1, 'el' + i);
                equal(this, window, 'this refers to window');

                array[index] = el + 1;
            });
            deepEqual(names, [2,3,4], '改变原数组');
            equal(ret, undefined, '返回值为undefined');

            var ctx = {x:1};
            i = 1;
            names.forEach(function(el, index, array){
                equal(el, ++i, 'el' + i);
                equal(index, i - 2, 'el' + i);
                deepEqual(this, ctx, 'this refers to {x:1}');
            }, ctx);
        });
        test('Array.prototype.map', function(){
            var names = [1,2,3];
            var i = 0;
            var ret = names.map(function(el, index, array){
                equal(el, ++i, 'el' + i);
                equal(index, i - 1, 'el' + i);
                equal(this, window, 'this refers to window');
            });
            deepEqual(ret, [undefined, undefined, undefined], '返回值为undefined数组');
            i = 0;
            var ctx = {x:1};
            ret = names.map(function(el, index, array){
                equal(el, ++i, 'el' + i);
                equal(index, i - 1, 'el' + i);
                deepEqual(this, ctx, 'this refers to {x:1}');

                return false;
            }, ctx);
            deepEqual(ret, [false, false, false], '返回值为false数组');
        });
        test('Array.prototype.filter', function(){
            var names = [1,2,3];
            var i = 0;
            var ret = names.filter(function(el, index, array){
                equal(el, ++i, 'el' + i);
                equal(index, i - 1, 'el' + i);
                equal(this, window, 'this refers to window');

                if (index === 1){
                    return false;
                }
                return 1;
            });
            deepEqual(ret, [1, 3], '返回值为[2,4]');

            i = 0;
            var ctx = {x:1};
            var ret = names.filter(function(el, index, array){
                equal(el, ++i, 'el' + i);
                equal(index, i - 1, 'el' + i);
                deepEqual(this, ctx, 'this refers to {x:1}');

                if (index === 1){
                    return false;
                }
                return true;
            }, ctx);
            deepEqual(ret, [1, 3], '返回值为[2,4]');
        });
        test('Array.prototype.some', function(){
            var names = [1,2,3];
            var ret = names.some(function(el, index, array){
                equal(this, window, 'this refers to window');
                return el < 3;
            });
            deepEqual(ret, true, '筛选3以下');
            var ctx = {x:1};
            ret = names.some(function(el, index, array){
                deepEqual(this, ctx, 'this refers to {x:1}');
                return el > 3;
            }, ctx);
            deepEqual(ret, false, '筛选3以上');
        });
        test('Array.prototype.every', function(){
            var names = [1,2,3];
            var ret = names.every(function(el, index, array){
                equal(this, window, 'this refers to window');
                return el < 3;
            });
            deepEqual(ret, false, '筛选3以下');
            var ctx = {x:1};
            ret = names.every(function(el, index, array){
                deepEqual(this, ctx, 'this refers to {x:1}');
                return el <= 3;
            }, ctx);
            deepEqual(ret, true, '筛选小于等于3');
        });
        test('Array.prototype.reduce', function(){
            var names = [1,2,3];
            var i = 0;
            var ret = names.reduce(function(p, c, index, array){
                equal(index, ++i, 'el' + i);
                deepEqual(array, [1,2,3], 'array' + i);
                deepEqual(this, window, 'this refers to window');

                return p + c;
            });
            equal(ret, 6, '结果为个各项之和');

            var ctx = {x:1};
            i = 0;
            ret = names.reduce(function(p, c, index, array){
                equal(index, i++, 'el' + i);
                deepEqual(array, [1,2,3], 'array' + i);
                deepEqual(this, window, 'this refers to window');

                return p + c;
            }, 1, ctx);
            equal(ret, 7, '结果为个各项之和 再 加上预设值');

            ret = [].reduce(function(){}, 1);
            equal(ret, 1, '数组为空时，返回预设值');
        });
        test('Array.prototype.reduceRight', function(){
            var names = [1,2,3];
            var i = 1;
            var j = 0;
            var ret = names.reduceRight(function(p, c, index, array){
                equal(index, i--, 'el' + (i+1));
                equal(c, 2 - j++, 'el' + j);
                deepEqual(array, [1,2,3], 'array' + (i+1));
                deepEqual(this, window, 'this refers to window');

                return p + c;
            });
            equal(ret, 6, '结果为个各项之和');

            var ctx = {x:1};
            i = 2;
            j = 0;
            ret = names.reduceRight(function(p, c, index, array){
                equal(index, i--, 'el' + (i+1));
                equal(c, 3 - j++, 'el' + j);
                deepEqual(array, [1,2,3], 'array' + (i+1));
                deepEqual(this, window, 'this refers to window');

                return p + c;
            }, 1, ctx);
            equal(ret, 7, '结果为个各项之和 再 加上预设值');

            ret = [].reduceRight(function(){}, 1);
            equal(ret, 1, '数组为空时，返回预设值');
        });

        test('Date.parse', function(){
            var now = new Date();
            equal(Date.parse(now.toISOString()) - now.getMilliseconds(), Date.parse(now.toGMTString()), '时间相同');

            var n = new Date('2014-05-15T10:10:10Z');
            equal(n.toISOString(), '2014-05-15T10:10:10.000Z', 'ISOString');

            n = new Date('2014-05-15T10:10:10+0800');
            equal(n.toISOString(), '2014-05-15T02:10:10.000Z', 'ISOString+0800');
            n = new Date('2014-05-15T10:10:10+08:10');
            equal(n.toISOString(), '2014-05-15T02:00:10.000Z', 'ISOString+08:10');

            n = new Date('2014-05-15T02:10:10-0800');
            equal(n.toISOString(), '2014-05-15T10:10:10.000Z', 'ISOString-0800');
            n = new Date('2014-05-15T02:10:10-08:10');
            equal(n.toISOString(), '2014-05-15T10:20:10.000Z', 'ISOString-08:10');
        });
    </script>
</body>
</html>